<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Blog_model extends CI_Model{

    /**
     * 添加
     * @param $data
     * @return mixed
     */
    public function add($data){
        $this->db->insert('blog',$data);
        return $data;
    }

    /**
     * （分类）获取博客表
     * @param $tid
     * @return mixed
     */
    public function getlist($tid = 0){
        if ($tid != 0)
            $this->db->where('tid', $tid);
        $data = $this->db->get('blog')->result_array();
        return $data;
    }

    /**
     * 分页（分类）获取博客表
     * @param int $tid
     * @param int $rows
     * @param int $offset
     * @return mixed
     */
    public function pagelist($tid = 0, $rows = -1, $offset = -1){
        if ($rows != -1 && $offset != -1)
            $this->db->limit($rows, $offset);
        if ($tid != 0)
            $this->db->where('tid', $tid);
        $data = $this->db->get('blog')->result_array();
        return $data;
    }

    /**
     * 返回查询行数
     * @param int $tid
     * @return mixed
     */
    public function all_rows($tid = 0){
        if ($tid != 0)
            $this->db->where('tid', $tid);
        $this->db->from('blog');
        return $this->db->count_all_results();
    }

    /**
     * 获取单条记录
     * @param $id
     * @return mixed
     */
    public function get($id){
        $data = $this->db->where(array('id' => $id))->get('blog')->row_array();
        return $data;
    }

    /**
     * 更新
     * @param $id
     * @param $data
     */
    public function update($id,$data){
        $this->db->update('blog',$data,array('id'=>$id));
    }

    /**
     * 删除
     * @param $id
     */
    public function del($id){
        $this->db->delete('blog',array('id' => $id));
    }

    public function search($word){
        return $this->db->query("select * from blog where title like '%".$word."%'")->result_array();
    }
}